/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.insurance.cis.controllers;

import com.insurance.cis.daos.PromotionDao;
import com.insurance.cis.models.Promotion;
import com.insurance.cis.scos.PromotionSearchCondition;
import com.insurance.cis.utils.MessageDialogUtils;
import com.insurance.cis.views.PromotionJDialogChooser;
import java.util.List;

/**
 *
 * @author TruongLQ
 */
public class PromotionChooserController {
    
    final PromotionJDialogChooser promotionDialog;
    final PromotionDao promotionDao = new PromotionDao();
    
    public PromotionChooserController(javax.swing.JDialog promotionDialog) {
        this.promotionDialog = (PromotionJDialogChooser)promotionDialog;
    }
    
    public PromotionSearchCondition validateSearchConditions() {
        Integer promotionID = null;
        if (!promotionDialog.getTxtPromotionID().getText().trim().isEmpty()) {
            try {
                promotionID = Integer.parseInt(promotionDialog.getTxtPromotionID().getText().trim());
            } catch (NumberFormatException ex) {
                MessageDialogUtils.dialogError(promotionDialog, "Promotion ID must be numeric!");
                promotionDialog.getTxtPromotionID().requestFocus();
                return null;
            }
        }
        Integer discount = null;
        if (!promotionDialog.getTxtDiscount().getText().trim().isEmpty()) {
            try {
                discount = Integer.parseInt(promotionDialog.getTxtDiscount().getText().trim());
            } catch (NumberFormatException ex) {
                MessageDialogUtils.dialogError(promotionDialog, "Discount must be numeric!");
                promotionDialog.getTxtDiscount().requestFocus();
                return null;
            }
        }
        
        return new PromotionSearchCondition(promotionID, discount, 
                promotionDialog.getTxtGift().getText().trim());
    }
    
    public List<Promotion> searchPromotions(PromotionSearchCondition sco) {
        return promotionDao.getPromotions(sco);
    }
}
